package student;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class StudentTest {
	List<StudentModel> list = new ArrayList<>();
	StudentDao dao =new StudentDao();
	Scanner sc = new Scanner(System.in);
	void start() {
		while (true) {
			System.out.println("-------------------------");
			System.out.println("---请选择您需要的功能:----");
			System.out.println("---1.录入学生信息---------");
			System.out.println("---2.查询全部学生信息-----");
			System.out.println("---3.按姓名查询学生信息---");
			System.out.println("---4.按专业查询学生信息---");
			System.out.println("---5.按学号添加学生信息---");
			System.out.println("---0.结束运行此程序-------");
			System.out.println("-------------------------");
			String str = sc.nextLine();
			switch (str) {
			case "1":
				input();
				break;
			case "2":
				output();
				break;
			case "3":
				inquireName();
				break;
			case "4":
				inquireDis();
				break;
			case "5":
				addInquire();
				break;
			case "0":
				System.out.println("程序已结束");
				return;
			default:
				System.out.println("输入错误，请重新尝试：");
			}

		}
	}
	
	/**
	 * 录入信息
	 */
	void input() {
		while (true) {
			StudentModel stu = new StudentModel();
			System.out.println("请录入学生学号：");
			stu.setId(sc.nextLine());
			System.out.println("请录入学生姓名：");
			stu.setName(sc.nextLine());
			System.out.println("请录入学生专业：");
			stu.setDis(sc.nextLine());
			int result = dao.insert(stu);
			if (result == 1) {
				System.out.println("添加成功");
			} else {
				System.out.println("添加失败");
			}
			System.out.println("请确认是否要继续录入(y/n)");
			String str = sc.nextLine();
			if (str.equalsIgnoreCase("n")) {
				break;
			}
		}
	}
	
	/**
	 * 查看全部学生信息
	 */
	void output() {
		StudentModel model = new StudentModel();
		list = dao.selectList(model);
		if (list.size() == 0) {
			System.out.println("暂无学生信息");
		} else {
			for (StudentModel stu : list) {
				outPut1(stu);
			}
		}
	}

	/**
	 * 输出一个学生信息
	 * 
	 * @param stu
	 */
	void outPut1(StudentModel stu) {
		System.out.println("学号：" + stu.getId());
		System.out.println("姓名：" + stu.getName());
		System.out.println("专业：" + stu.getDis());
	}

	/**
	 * 按姓名查询学生信息
	 */
	void inquireName() {
		StudentModel model = new StudentModel();
		System.out.println("请输入想要查询的学生姓名：");
		String str = sc.nextLine();
		model.setName(str);
		List<StudentModel> list = dao.selectList(model);
		for (StudentModel stu : list) {
			outPut1(stu);
		}
	}

	/**
	 * 按专业查询学生信息
	 */
	void inquireDis() {
		StudentModel model = new StudentModel();
		System.out.println("请输入想要查询的专业：");
		String str = sc.nextLine();
		model.setDis(str);
		List<StudentModel> list = dao.selectList(model);
		for (StudentModel stu : list) {
			outPut1(stu);
		}
	}

	/**
	 * 修改专业
	 */
	void addInquire() {
		while (true) {
			StudentModel model = new StudentModel();
			System.out.println("请输入想要修改专业的学生学号：");
			String str = sc.nextLine();
			model.setId(str);
			System.out.println("请输入想要修改的专业");
			String str2 = sc.nextLine();
			model.setDis(str);
			int result = dao.update(model);
			if (result == 1) {
				System.out.println("修改成功");
			} else {
				System.out.println("修改失败");
			}
			System.out.println("请确认是否要继续录入(y/n)");
			String in = sc.nextLine();
			if (in.equalsIgnoreCase("n")) {
				break;
			}
		}
	}
	
	public static void main(String[] args) {
		StudentTest stuT = new StudentTest();
		stuT.start();
	}
	
}
